package hu.myandroidlife.base;

import hu.myandroidlife.R;
import hu.myandroidlife.util.ActivityCollector;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

public abstract class BaseActivity extends Activity implements OnClickListener {

	protected static final int SHOW_TIME = Toast.LENGTH_SHORT;
	
	//当前Activity名字 
    protected static String ACTIVITY_NAME;
    protected Context context;

	// 定义抽象方法
	protected abstract int getLayoutId();
	protected abstract void initView();
	protected abstract void initListener();
	protected abstract void widgetClick(View v);

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
//		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(getLayoutId());
		
		ActivityCollector.addActivity(this);
		context = this;
		ACTIVITY_NAME=getClass().getSimpleName();
		

		
		initView();
		initListener();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();

		ActivityCollector.removeActivity(this);
	}


	@Override
	public void onClick(View v) {
		if ($fastClick())
			widgetClick(v);
	}

	private long lastClick = 0;

	private boolean $fastClick() {
		if (System.currentTimeMillis() - lastClick <= 200) {
			$showMsg(R.string.base_base_fastclick);
			return false;
		}
		lastClick = System.currentTimeMillis();
		return true;
	}

	/**
	 * findViewById
	 */
	public <T extends View> T $findViewById(int pResId) {
		return (T) findViewById(pResId);
	}

	/**
	 * 打印log
	 */
	protected boolean isDebug = true;

	protected void $log(String pMsg) {
		if (isDebug) {
			Log.e(this.getClass().getName(), pMsg);
		}
	}

	/**
	 * 显示一个Toast信息
	 * 
	 * @param pMsg
	 */
	private Toast toast = null;

	/** 不会一直重复重复重复重复的提醒了 */
	protected void $showMsg(String pMsg) {
		if (toast == null) {
			toast = Toast.makeText(this, pMsg, SHOW_TIME);
		} else {
			toast.setText(pMsg);
		}
		toast.show();
	}

	/**
	 * 显示一个Toast信息
	 * 
	 * @param pResID
	 */
	protected void $showMsg(int pResID) {
		if (toast == null) {
			toast = Toast.makeText(this, pResID, SHOW_TIME);
		} else {
			toast.setText(pResID);
		}
		toast.show();
	}

	/**
	 * 界面跳转,无返回
	 */
	protected void $openActivity(Class<?> pClass, Bundle pBundle) {
		Intent _Intent = new Intent();
		_Intent.setClass(this, pClass);
		if (pBundle != null) {
			_Intent.putExtras(pBundle);
		}

		startActivity(_Intent);
	}

	/**
	 * 界面跳转,有返回
	 */
	protected void $openActivity(Class<?> pClass, Bundle pBundle,
			int pRequestCode) {
		Intent _Intent = new Intent(this, pClass);
		if (pBundle != null) {
			_Intent.putExtras(pBundle);
		}
		startActivityForResult(_Intent, pRequestCode);
	}

	protected Bundle $getIntentExtra() {
		Intent _Intent = getIntent();
		Bundle _Bundle = null;
		if (null != _Intent)
			_Bundle = _Intent.getExtras();
		return _Bundle;
	}

	// //////////////////////////////////////////////////////

	/**
	 * 使状态栏的背景色和标题栏颜色一致
	 */
	protected void $makeTitleBarTransparent() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
			Window window = getWindow();
			window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
					WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
		}
	}

	/**
	 * 隐藏软键盘
	 */
	protected void $hideKeyboard() {
		InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		if (getWindow().getAttributes().softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) {
			if (getCurrentFocus() != null)
				manager.hideSoftInputFromWindow(getCurrentFocus()
						.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
		}
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if(event.getAction()==MotionEvent.ACTION_DOWN){
			$hideKeyboard();
			return true;
		}
		return super.onTouchEvent(event);
	}

}
